﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NGourd.CLI
{
    abstract class Options
    {
        public readonly static Options Instance = new NGourdOptions();

        private IList<Option> options = new List<Option>();
        protected void AddOption(Option opt)
        {
            options.Add(opt);
        }

        public Option GetOptionByName(string optionName)
        {
            return options.SingleOrDefault(
                        x => String.Equals(optionName, x.LongForm, StringComparison.InvariantCultureIgnoreCase)
                          || String.Equals(optionName, x.ShortForm, StringComparison.InvariantCultureIgnoreCase)
                    );

        }

        public IConfigurationTweak GetTweakForOption(string optionName, string value)
        {
            var option = GetOptionByName(optionName);
            if (option == null)
                throw new Exception(optionName + "is not a defined option");

            return option.CreateTweak(value);
        }

        public virtual void ShowUsage()
        {
            Console.WriteLine("Options");
            foreach (var option in options)
            {
                Console.WriteLine("\n\n-{0}:value, --{1}:value\n{2}", option.ShortForm, option.LongForm, option.Description);
            }
        }
    }

}
